﻿using Ec_System.Service.Web;
using Ec_System.Models.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ec_System.API;

namespace Ec_System.Bll.Web
{
    public class CustomerManager
    {
        CustomerService customerService = new CustomerService();

        // <summary>
        /// 检测用户名是否存在
        /// </summary>
        /// <param name="loginID"></param>
        /// <returns>True -- 存在</returns>
        public bool GetCustomerCountByLoginID(string loginID)
        {
            return customerService.GetCustomerCountByLoginID(loginID) > 0;
        }

        /// <summary>
        /// 检测Email是否存在
        /// </summary>
        /// <param name="email"></param>
        /// <returns>True -- 存在</returns>
        public bool GetCustomerCountByEmail(string email)
        {
            return customerService.GetCustomerCountByEmail(email) > 0;
        }

        /// <summary>
        /// 检测电话号码是否存在
        /// </summary>
        /// <param name="email"></param>
        /// <returns>True -- 存在</returns>
        public bool GetCustomerCountByTel(string tel)
        {
            return customerService.GetCustomerCountByTel(tel) > 0;
        }

        /// <summary>
        /// 讲注册的用户信息入库
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddCustomer(CustomerData model)
        {
            return customerService.AddCustomer(model);
        }

        /// <summary>
        /// 用户登录验证
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CustomerData UserLoginValidate(CustomerData model)
        {
            //使用MD5加密
            model.Password = MD5Helper.EncryptString(model.Password);

            return customerService.UserLoginValidate(model);
        }

        /// <summary>
        /// 根据LoginID获取用户信息
        /// </summary>
        /// <param name="loginID"></param>
        /// <returns></returns>
        public CustomerData GetUserInfoByLoginID(string loginID)
        {
            CustomerAddressManager adrManager = new CustomerAddressManager();

            var model = customerService.GetUserInfoByLoginID(loginID);            
            //获取收货地址
            model.CustomerAdress = adrManager.GetCustomerAdrListByLoginID(loginID);

            return model;
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateUserInfoByLoginID(CustomerData model)
        {
            return customerService.UpdateUserInfoByLoginID(model);
        }

        /// <summary>
        /// 更新用户密码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateUserPwdByLoginID(CustomerData model)
        {
            //使用MD5加密
            model.Password = MD5Helper.EncryptString(model.Password);

            return customerService.UpdateUserPwdByLoginID(model);
        }

        /// <summary>
        /// 根据LoginID获取用户信息及其收货地址
        /// </summary>
        /// <param name="loginID"></param>
        /// <returns></returns>
        public CustomerData GetUserInfoAndAddressInfoByLoginID(string loginID)
        {
            return customerService.GetUserInfoAndAddressInfoByLoginID(loginID); 
        }
    }
}
